package com.itheima.tlias.controller;

import com.itheima.tlias.pojo.Dept;
import com.itheima.tlias.pojo.Result;
import com.itheima.tlias.service.DeptService;
import com.itheima.tlias.utils.CurrentHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/depts")
@Slf4j
public class DeptController {
	@Autowired
	private DeptService deptService;

	@GetMapping
	public Result list() {
		List<Dept> list = deptService.list();
		return Result.success(list);
	}


	//@DeleteMapping
	//public Result delete(@RequestParam(value = "ID", required = false) Integer id) {
	//	System.out.println(id);
	//	//return deptService.deleteById(id) > 0 ? Result.success() : Result.error("没有该部门id，删除失败");
	//	return Result.success();
	//}

	//@DeleteMapping
	//public Result delete(HttpServletRequest request) {
	//	Integer id = Integer.parseInt(request.getParameter("id"));
	//	System.out.println(id);
	//	return deptService.deleteById(id) > 0 ? Result.success() : Result.error("没有该部门id，删除失败");
	//	//return Result.success();
	//}

	@DeleteMapping
	public Result delete(Integer id) {
		return deptService.deleteById(id) > 0 ? Result.success() : Result.error("没有id为" + id + "的部门，删除失败");
	}

	@PostMapping
	public Result save(@RequestBody Dept dept) {
		deptService.save(dept);
		log.info("员工id===================》{}", CurrentHolder.getCurrentId());
		return Result.success();
	}

	@GetMapping("/{id}")
	public Result getById(@PathVariable("id") Integer id) {
		Dept dept = deptService.getById(id);
		return Result.success(dept);
	}

	@PutMapping
	public Result update(@RequestBody Dept dept) {
		deptService.updateById(dept);
		return Result.success();
	}


}
